import http from '@/utils/http'
import { useAuthStore } from '@/store/auth'
import { storeToRefs } from 'pinia'

// 获取当前用户的企业编号
const  authStore  = useAuthStore()
const { globalParams } = storeToRefs(authStore)
// 分页参数接口
interface PageParams {
  page?: number
  size?: number
  [key: string]: any
}

// 告警列表响应接口
interface AlarmResponse {
  code: number
  data: {
    list: any[]
    total: number
    [key: string]: any
  }
  message: string
}

/**
 * 获取告警列表
 * @param params 查询参数
 * @returns Promise<AlarmResponse>
 */
export const getAlarmList = (params: PageParams): Promise<AlarmResponse> => {
  return http.postOrJson('/alarm/dataList', { ...globalParams.value, ...params })
}

/**
 * 获取当前告警数量
 * @param params 查询参数
 * @returns Promise<AlarmResponse>
 */
export const getAlarmVoiceData = (): Promise<AlarmResponse> => {
  return http.get('/alarm/voiceData', { params: { ...globalParams.value } })
}

/**
 * 获取告警详情
 * @param id 告警ID
 * @returns Promise<AlarmResponse>
 */
export const getAlarmHistory = (id: string): Promise<AlarmResponse> => {
  return http.get(`/monitor/getAlarmHistory/${id}`)
}

/**
 * 获取企业通讯录列表
 * @returns Promise<AlarmResponse>
 */
export const getAddressBookList = (): Promise<AlarmResponse> => {
  return http.postOrJson('/monitor/maillist/dataList', { ...globalParams.value })
}

/**
 * 获取传感器历史告警列表
 * @param params 查询参数
 * @returns Promise<AlarmResponse>
 */
export const getSensorAlarmList = (params: PageParams): Promise<AlarmResponse> => {
  return http.postOrJson('monitor/dataList', { ...globalParams.value, ...params })
}

/**
 * 获取传感器标校记录
 * @param params 查询参数
 * @returns Promise<AlarmResponse>
 */
export const getSensorCalibrationList = (params: PageParams): Promise<AlarmResponse> => {
  return http.postOrJson('monitor/bxDataList', { ...globalParams.value, ...params })
}

/**
 * 获取未读告警列表
 * @param params 查询参数
 * @returns Promise<AlarmResponse>
 */
export const getUnreadAlarmList = (params: PageParams): Promise<AlarmResponse> => {
  return http.postOrJson('/alarm/noRead/dataList', { ...globalParams.value, ...params })
}

/**
 * 获取告警指标列表
 * @returns Promise<AlarmResponse>
 */
export const getAlarmQuotaList = () => {
  return http.get('/alarm/quotaList')
}

/**
 * 获取需要展示的传感器列表
 * @returns Promise<AlarmResponse>
 */
export const getNeedShowSensor = (cdCode: string) => {
  return http.get('/alarm/getNeedShowSensor', {
    params: {...globalParams.value, cdCode}
  })
}
